// components/imageUploaded.js

Component({
  behaviors: ['wx://form-field'],
  /**
   * 组件的属性列表
   */
  properties: {
  },
  /**
   * 组件的初始数据
   */
  data: {
    "images":[],
    "imageWidth": wx.getSystemInfoSync().windowWidth / 4 - 10
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 选择图片
    chooseImage: function (e) {
      let that = this;
      // 选择图片
      wx.chooseImage({
        count: 9, // 默认9
        sizeType: ['compressed'],
        // 可以指定来源是相册还是相机，默认二者都有
        sourceType: ['album', 'camera'],
        success: function (res) {
          console.log(res);
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          let tempFilePaths = res.tempFilePaths;
          let images = that.data.images.concat(tempFilePaths);
          that.setData({ 
            value: images,      //存储到组件value中
            images: images      //页面Array显示
          });
        }
      })
    },
    //图片预览
    previewImage: function (e) {
      let that = this;
      let current = e.target.dataset.src;
      wx.previewImage({
        current: current,
        urls: that.data.images,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    },

    //删除照片
    delete: function (e) {
      console.log(e);
      let that = this;
      let index = e.currentTarget.dataset.index; 
      let images = that.data.images;
      images.splice(index, 1);
      that.setData({
        value: images,      //存储到组件value中
        images: images      //页面Array显示
      });
    }
  }
})
